<?php

namespace App\Library\Logic;

use App\Models\PaymentRelatedModel;
use Illuminate\Database\Eloquent\Model;

class PaymentRelatedLogic extends PaymentRelatedModel
{
    /**
     * add
     * @param array $where
     * @return Model
     */
    public static function add(array $where): Model
    {
        return self::insertForModel(array_merge($where, ['config' => config('app.wechat')]));
        /*$paymentRelated = self::query()->where(humpToLine($where))->first();
        if (!is_null($paymentRelated)) return $paymentRelated;
        else return self::insertForModel(array_merge($where, ['config' => config('app.wechat')]));*/
    }

    /**
     * addByProductOrder
     * @param Model $order
     * @param int $paymentMethodId
     * @return Model
     */
    public static function addByProductOrder(Model $order, int $paymentMethodId = 2): Model
    {
        $data = [
            'objectType' => $order->getTable(),
            'objectId' => $order->order_id,
            'paymentMethodId' => $paymentMethodId,
            'method' => 1,
            'status_pay' => $paymentMethodId === 4 ? 1 : 0,
        ];
        return self::insertForModel(array_merge($data, ['config' => config('app.wechat')]));
        /*$paymentRelated = self::findByWhere($data);
        if (!is_null($paymentRelated)) return $paymentRelated;
    else return self::insertForModel(array_merge($data, ['config' => config('app.wechat')]));*/
    }

    /**
     * paySuccess
     * @param int $id
     * @return int
     */
    public static function paySuccess(int $id): int
    {
        return self::query()->where('id', $id)->where('status_pay', 0)->update(['status_pay' => 1]);
    }
}
